$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'warn/brfidwarn/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,align:"center"},
			/*{ label: '仓房名称', name: 'antennaName', index: 'antenna_name', width: 80 }, 			
			{ label: '仓房编号', name: 'unitCode', index: 'unit_code', width: 80 }, */			
			{ label: '工具名称', name: 'goodsName', index: 'goodsName', width: 80 ,align:"center"}, 			
			{ label: '报警批次', name: 'batch', index: 'batch', width: 80 ,align:"center"}, 			
			/*{ label: '备注', name: 'remark', index: 'remark', width: 80 },*/ 			
			{ label: '报警原因', name: 'warnReason', index: 'warn_reason', width: 80 ,align:"center"}, 			
			/*{ label: '创建人', name: 'updateUserId', index: 'update_user_id', width: 80 },*/ 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,align:"center", formatter:function(value,options,row){
                return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}},		
			/*{ label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 }, 			
			{ label: '是否启用,0未启用，1启用', name: 'isUsed', index: 'is_used', width: 80 }, 			
			{ label: '是否删除，1删除，0未删除', name: 'isDel', index: 'is_del', width: 80 }, 			
			{ label: '', name: 'lableCode', index: 'lable_code', width: 80 }*/			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "rfidWarnList.list",
            page: "rfidWarnList.pageNum",
            total: "rfidWarnList.pages",
            records: "rfidWarnList.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bRfidWarn: {},
		msg:"010500000000CDCA",
        showConf:true,
        bRfidWarnConf:{
			type:[]
		},
        warnConf:[]
	},
	mounted:function(){
        this.queryConfList();
	},
	methods: {
		queryConfList:function(){
            $.get(baseURL + "warn/brfidwarn/queryConfList", function(r){
                vm.warnConf = r.warnConf;
            });
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bRfidWarn = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bRfidWarn.id == null ? "warn/brfidwarn/save" : "warn/brfidwarn/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bRfidWarn),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "warn/brfidwarn/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "warn/brfidwarn/info/"+id, function(r){
                vm.bRfidWarn = r.bRfidWarn;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		closeWarn: function (event) {
			var msg = {"msg":vm.msg};
			$.ajax({
				type: "POST",
			    url: baseURL + 'sendmsg',
                contentType: "application/json",
			    data: JSON.stringify(msg),
			    success: function(r){
			    	if(r.code === 0){
						alert('关闭成功');
					}else{
						alert(r.msg);
					}
				}
			});
		},
        warnConfig:function () {

            $.get(baseURL + "warn/brfidwarnconf/info/", function(r){
                var result = r.bRfidWarnConf;
                if(result==null){
                    vm.bRfidWarnConf.type=[];
                    vm.bRfidWarnConf.id='';
                }else{
                    vm.bRfidWarnConf=result;
                    var type=result.type.split(",");
                    var list=[];
                    for (var i = 0; i < type.length; i++) {
						list.push(parseInt(type[i]))
                    }
                    vm.bRfidWarnConf.type=list;
                   // vm.$set(vm.bRfidWarnConf,'type',type);

                }
                vm.showConf=false;
                vm.showList=false;
            });
        },
        saveConf:function () {
			var type=vm.bRfidWarnConf.type.join(',');
            $.post(baseURL + "warn/brfidwarnconf/saveOrUpdate",{
            	'type':type,
            	'id':vm.bRfidWarnConf.id,
			}, function(r){
				if(r.code==0){
                    layer.msg("操作成功")
                    vm.showConf=true;
                    vm.showList=true;
				}else{
                    layer.msg("操作失败，请联系管理员")

				}

            });
        }
	}
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}